<?php
/**
 * @version $Id: options.php 1202 2010-05-17 03:46:25Z elkuku $
 * @package		EasyCreator
 * @subpackage	Templates
 * @author		EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author		Nikolai Plath (elkuku) {@link http://www.nik-it.de NiK-IT.de}
 * @author		Created on 09-May-2009
 */

//-- No direct access
defined('_JEXEC') or die('=;)');

/**
 * Custom options for EasyCreator extension templates.

 * @package     EasyCreator
 */
class EasyTemplateOptions
{
    private $reservedNames = array();

    public function __construct()
    {
        switch(ECR_JVERSION)
        {
        	case '1.6':
        	    $this->reservedNames = array('joomla', 'bitfolge', 'phpmailer', 'phputf8', 'simplepie');
        	break;

        	default:
        		;
        	break;
        }//switch
    }//function
    /**
     * Displays available options with nput fields.
     *
     * @return string HTML
     */
    public function displayOptions()
    {
        $exclude = array_merge(array('.svn', 'CVS','.DS_Store','__MACOSX'), $this->reservedNames);

        $libraries = JFolder::folders(JPATH_LIBRARIES, '.', false, false, $exclude);

        $html = '';

        $select = '';
        $select .= '<select onchange="$(\'ecr_folder_name\').value = this.value;">';
        $select .= '<option>Custom</option>';

        foreach($libraries as $library)
        {
        	$select .= '<option>'.$library.'</option>';;
        }//foreach

        $select .= '</select>';

        $html .= JText::sprintf('Existing libraries: %s_select', $select);

        $html .= JText::_('Folder name').' : <input type="text" name="ecr_folder_name" id="ecr_folder_name" />';

        return $html;
    }//function

    /**
     * Get the required fields.
     *
     * @return array Required fields.
     */
    public function getRequireds()
    {
        $requireds = array('ecr_folder_name');

        return $requireds;
    }//function

    /**
     * Process custom options.
     *
     * @param object $easyBuilder EasyBuilder
     *
     * @return boolean True on sucess.
     */
    public function processOptions(EasyBuilder $EasyBuilder)
    {
        $ecr_folder_name = JRequest::getCmd('ecr_folder_name');

        if( ! $ecr_folder_name)
        {
            JError::raiseWarning(100, JText::_('No folder given'));

            return false;
        }

        if(in_array($ecr_folder_name, $this->reservedNames))
        {
            JError::raiseWarning(100, JText::sprintf('%s is a reserved name', $ecr_folder_name));

            return false;
        }

        $EasyBuilder->setScope(strtolower($ecr_folder_name));

        $EasyBuilder->addSubstitute('_ECR_COM_SCOPE_', ucfirst($ecr_folder_name));

        return true;
    }//function

}//class
